from import_export import resources
from import_export.fields import Field

from pms import models
from pms.signals import import_th_done


class BaseTargetTypeResource(resources.ModelResource):
    """测点类型数据导出信息"""
    id = Field(attribute='id', column_name='类型id')
    tag = Field(attribute='tag', column_name='类型标签（英文简写）')
    name = Field(attribute='name', column_name='类型名称')
    remarks = Field(attribute='remarks', column_name='备注')

    def __init__(self, input_contract=None):
        super(BaseTargetTypeResource, self).__init__()
        field_list = models.BaseTargetType._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    def get_import_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    class Meta:
        model = models.BaseTargetType
        fields = ['id', 'tag', 'name', 'remarks']
        export_order = ('id', 'tag', 'name', 'remarks')
        import_id_fields = ('id', 'tag')


class SettingsDataQuotaResource(resources.ModelResource):
    """指标数据导出信息"""
    target_type_id = Field(attribute='target_type_id', column_name='类型id')
    code = Field(attribute='code', column_name='指标编号')
    name = Field(attribute='name', column_name='指标名称')
    unit = Field(attribute='unit', column_name='指标单位')
    display = Field(attribute='display', column_name='是否显示')
    remarks = Field(attribute='remarks', column_name='备注')

    def __init__(self, input_contract=None):
        super(SettingsDataQuotaResource, self).__init__()
        field_list = models.SettingsDataQuota._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    def get_import_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    class Meta:
        model = models.SettingsDataQuota
        fields = ['target_type_id', 'code', 'name', 'unit', 'display', 'remarks']
        export_order = ('target_type_id', 'code', 'name', 'unit', 'display', 'remarks')
        import_id_fields = ['target_type_id', 'code']


class BaseTargetSiteGroupResource(resources.ModelResource):
    id = Field(attribute='id', column_name='分区id')
    name = Field(attribute='name', column_name='分区名称')

    def __init__(self, input_contract=None):
        super(BaseTargetSiteGroupResource, self).__init__()
        field_list = models.BaseTargetSiteGroup._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    def get_import_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    class Meta:
        model = models.BaseTargetSiteGroup
        fields = ['id', 'name']
        export_order = ('id', 'name')
        import_id_fields = ('id',)


class BaseTargetSiteResource(resources.ModelResource):
    name = Field(attribute='name', column_name='位置名称')
    project_id = Field(attribute='project_id', column_name='所属项目id')
    group_id = Field(attribute='group_id', column_name='分区id')

    def __init__(self, input_contract=None):
        super(BaseTargetSiteResource, self).__init__()
        field_list = models.BaseTargetSite._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    def get_import_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    class Meta:
        model = models.BaseTargetSite
        fields = ['name', 'project_id', 'group_id']
        export_order = ('name', 'project_id', 'group_id')
        import_id_fields = ('name',)


class BaseTargetResource(resources.ModelResource):
    project_id = Field(attribute='project_id', column_name='项目id')
    type_id = Field(attribute='type_id', column_name='类型id')
    site_id = Field(attribute='site_id', column_name='位置id')
    serial_number = Field(attribute='serial_number', column_name='通讯编号(采集id)')
    code = Field(attribute='code', column_name='测点编号')
    demodulator = Field(attribute='demodulator', column_name='解调仪编号')
    channel = Field(attribute='channel', column_name='通道号')
    initial = Field(attribute='initial', column_name='初值')
    ratio = Field(attribute='ratio', column_name='转换系数')

    def __init__(self, input_contract=None):
        super(BaseTargetResource, self).__init__()
        field_list = models.BaseTarget._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    def get_import_fields(self):
        fields = self.get_fields()
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
        return fields

    class Meta:
        model = models.BaseTarget
        fields = ['project_id', 'type_id', 'site_id', 'serial_number', 'code', 'demodulator', 'channel', 'initial',
                  'ratio']
        exclude = ('id', 'name')
        export_order = ('project_id', 'type_id', 'site_id', 'serial_number', 'code', 'demodulator', 'channel',
                        'initial', 'ratio')
        import_id_fields = ['project_id', 'serial_number', 'demodulator', 'channel']


class SettingsThresholdResource(resources.ModelResource):
    target_id = Field(attribute='target_id', column_name='测点id')
    quota_id = Field(attribute='quota_id', column_name='指标id')
    level = Field(attribute='level', column_name='预警等级(1紧急，2严重，3一般)')
    lower = Field(attribute='lower', column_name='阈值下限')
    upper = Field(attribute='upper', column_name='阈值上限', default=999999)
    absolute = Field(attribute='absolute', column_name='是否取绝对值(1是，0否)')

    def __init__(self, input_contract=None):
        super(SettingsThresholdResource, self).__init__()
        field_list = models.SettingsThreshold._meta.fields
        self.verbose_name_dict = {}
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    # def get_export_fields(self):
    #     fields = self.get_fields()
    #     for field in fields:
    #         field_name = self.get_field_name(field)
    #         if field_name in self.verbose_name_dict.keys():
    #             field.column_name = self.verbose_name_dict[field_name]
    #     return fields
    #
    # def get_import_fields(self):
    #     fields = self.get_fields()
    #     for field in fields:
    #         field_name = self.get_field_name(field)
    #         if field_name in self.verbose_name_dict.keys():
    #             field.column_name = self.verbose_name_dict[field_name]
    #     return fields

    def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
        print('--------after_import---------')
        import_th_done.send('th', type='import')
        # import_th_done.send('th', toppings=123, size=456)

    class Meta:
        model = models.SettingsThreshold
        fields = ['target_id', 'quota_id', 'level', 'lower', 'upper', 'absolute']
        exclude = ('id',)
        export_order = ('target_id', 'quota_id', 'level', 'lower', 'upper', 'absolute')
        import_id_fields = ('target_id', 'quota_id', 'level', 'lower')
